﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class SJZD_AddType : ATPages
{
    Function f = new Function();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            hidID1.Value = Request.QueryString["moduid"].ToString();
            lblTime.Text = DateTime.Now.ToShortDateString();
            lblUser.Text = Server.UrlDecode(Request.Cookies["user"].Values["realname"]);
            lblwz.Text = f.getModuleidAdd(Request["moduid"]);
            
            if (Request.QueryString["action"].ToString()=="update")
            {
               
                hidID1.Value = Request.QueryString["moduid"].ToString();
                hidId.Value = Request.QueryString["TypeId"].ToString();
           
                btnSave.Text = " 修改 ";
                ClientScript.RegisterStartupScript(typeof(Page), "aa", "dispAdd()", true);
            }
            if (Request.QueryString["action"].ToString() == "flag")
            {
                ZSql flag = new ZSql();
                hidID1.Value = Request.QueryString["moduid"].ToString();
                hidId.Value = Request.QueryString["TypeId"].ToString();
           
                btnSave.Text = " 审核 ";
                flag.Open("select * from ComoditiesType where TypeId='" + hidId.Value + "' ");
                if (flag.m_table.Rows[0]["AuditState"].ToString() == "1")
                {
                    rbtnFlag.Checked = true;
                    rbtnnotFlag.Checked = false;
                }
                if (flag.m_table.Rows[0]["AuditState"].ToString() == "0")
                {
                    rbtnFlag.Checked = false;
                    rbtnnotFlag.Checked = true;
                }
            }
            if (Request.QueryString["action"].ToString() == "add")
            {
              
                btnSave.Text = " 添加 ";
                ClientScript.RegisterStartupScript(typeof(Page), "aa", "dispAdd()", true);
            }
            
            initType();
        }
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        try
        {
            ZSql zs = new ZSql();
            if (!string.IsNullOrEmpty(hidId.Value))
            {
                int flag;
                if (rbtnFlag.Checked)
                {
                    flag = 1;
                }
                else
                {
                    flag = 0;
                }
                if (Request.QueryString["action"].ToString() == "flag")
                {

                    zs.Open("update ComoditiesType set AuditState=" + flag + ",usTime='" + lblTime.Text + "',UserName='"+lblUser.Text+"' where TypeId=" + hidId.Value);
                    Response.Write("<script>window.location='TypeManage.aspx?moduid=" + Request["moduid"] + "';</script>");

                }
                else
                {
                    
                    zs.Open("update ComoditiesType set ComoditiesType='" + Common.FormatParameter(txtType.Text) + "',TypeOrder='" + Common.FormatParameter(txtOrder.Text) + "',TypeDescription='" + Common.FormatParameter(txtDescription.Text) + "',usTime='" + lblTime.Text + "',UserName='" + lblUser.Text + "',typebh='"+Common.FormatParameter(txtbh.Text)+"' where TypeId=" + hidId.Value);
                   
                   
                    ClientScript.RegisterStartupScript(typeof(Page), "aa", "dispAdd()", true);
                    Response.Write("<script>window.location='TypeManage.aspx?moduid=" + Request["moduid"] + "';</script>");
                }
             
               
                
            
               

            }
            else
            {
                zs.Open("select * from ComoditiesType where ComoditiesType='" + Common.FormatParameter(txtType.Text)+ "'");
                if (zs.m_table.Rows.Count > 0)
                {



                    Response.Write("<script>alert('该类型已存在');location.href=document.referrer</script>");
                    ClientScript.RegisterStartupScript(typeof(Page), "aa", "dispAdd()", true);
                   
                    return;
                }

                zs.Open("insert into ComoditiesType (ComoditiesType,TypeOrder,TypeDescription,usTime,UserName,typebh) values('" + Common.FormatParameter(txtType.Text) + "','" + Common.FormatParameter(txtOrder.Text) + "','" + Common.FormatParameter(txtDescription.Text) + "','" + lblTime.Text + "','" + lblUser.Text + "','"+Common.FormatParameter(txtbh.Text)+"')");

               
                ClientScript.RegisterStartupScript(typeof(Page), "aa", "dispAdd()", true);
                Response.Write("<script>window.location='TypeManage.aspx?moduid=" + Request["moduid"] + "';</script>");
            }
            zs.Close();
        }
        catch (Exception)
        {

             Response.Write("<script>alert('添加失败');location.href=document.referrer</script>");
            ClientScript.RegisterStartupScript(typeof(Page), "aa", "dispAdd()", true);
        }
    }
    protected void Linkcannel_Click(object sender, EventArgs e)
    {
        Response.Redirect("TypeManage.aspx?moduid="+hidID1.Value);
    }
    private void initType()
    {
        ZSql type = new ZSql();
        if (!string.IsNullOrEmpty(hidId.Value))
        {
            type.Open("select ComoditiesType,TypeOrder,TypeDescription,AuditState,typebh from ComoditiesType where TypeId=" + hidId.Value);
            if (type.m_table.Rows[0]["AuditState"].ToString() == "1")
            {
                rbtnnotFlag.Checked = false;
                rbtnFlag.Checked = true;
            }
            txtType.Text = type.m_table.Rows[0]["ComoditiesType"].ToString();
            txtOrder.Text = type.m_table.Rows[0]["TypeOrder"].ToString();
            txtDescription.Text = type.m_table.Rows[0]["TypeDescription"].ToString();
            txtbh.Text = type.m_table.Rows[0]["typebh"].ToString();
            type.Close();
        }
    }
}
